package org.dreamwork.jasmine2.parser;

import org.dreamwork.text.TextParser;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.net.URL;

public class CSSReader extends TextParser {
    int quot_char = -1;

    public CSSReader (File file) throws IOException {
        super (file);
    }

    public CSSReader (URL url) throws IOException {
        super (url);
    }

    public CSSReader (InputStream in) throws IOException {
        super (in);
    }

    public CSSReader (String src) {
        super (src);
    }

    public CSSReader (Reader reader) throws IOException {
        super (reader);
    }

    public CSS parse () {
        CSS root = new CSS ("root");
        CSS[] css  = null;
        String attrName = null;
        Segment segment = new Segment (skipSpace (), 0);
        boolean record_css_name = true;
        for (int ch = nextChar (); ch != -1; ch = nextChar ()) {
            switch (ch) {
                case '{' :
                    if (quot_char < 0) { // CSS 名称结束，接着应该处理
                        segment.length = cursor - segment.start - 1;
                        String exp = segment.toString ().trim ();
                        exp = exp.replace (',', ' ').replaceAll ("\\s+", " ");
                        String[] names = exp.split (" ");
                        css = new CSS [names.length];
                        for (int i = 0; i < css.length; i ++) {
                            String name = names [i].trim ();
                            name = name.startsWith (".") ? name.substring (1) : name;
                            css [i] = new CSS (name);
                        }
                        segment.reset ();
                        record_css_name = false;
                    }
                    break;
                case '}' :
                    if (quot_char < 0) {
                        if (attrName != null) {
                            segment.length = cursor - segment.start - 1;
                            segment.start ++;
                            String attrValue = segment.toString ().trim ();
                            for (int i = 0; i < css.length; i ++) css [i].setAttribute (attrName, attrValue);
                            attrName = null;
                            segment.reset ();
                        }

                        for (int i = 0; i < css.length; i ++) root.addChild (css [i]);
                        css = null;
                        skipSpace ();
                        segment.reset ();
                        record_css_name = true;
                    }
                    break;
                case '/' :
                    if (quot_char < 0) {
                        if (peek () == '*') { // 注释开始，跳过
                            skipUntilOutQuot ("*/", false);
                            segment.reset ();
                        }
                    }
                    break;
                case ';' :
                    if (quot_char < 0) {
                        if (attrName != null) {
                            segment.length = cursor - segment.start - 1;
                            String attrValue = segment.toString ();
                            if (attrValue != null) attrValue = attrValue.trim ();
                            for (int i = 0; i < css.length; i ++) css [i].setAttribute (attrName, attrValue);
                            attrName = null;
                            segment.reset ();
                        }
                    }
                    break;
                case ':' :
                    if (quot_char < 0) {
                        if (!record_css_name) {
                            segment.length = cursor - segment.start - 1;
                            attrName = segment.toString ().trim ();
                            segment.reset ();
                            record_css_name = false;
                        }
                    }
                    break;
            }
        }

        return root;
    }

    public static CSS Parse (String str) {
        CSS css = new CSS ("");
        String attr = str.replaceAll ("\\s+", " ");
        String[] attrs = attr.split (";");
        for (int i = 0; i < attrs.length; i ++) {
            String[] kv = attrs [i].split (":");
            css.setAttribute (kv [0].trim (), kv [1].trim ());
        }
        return css;
    }

    public static void main (String[] args) throws Exception {
        CSSReader.Parse ("font-size:23px;\nfont-family:sanf;    color:red;background-color: blue");
    }
}